//
//  PhonePwdLoginViewModelSpec.swift
//  User
//
//  Created by ET|冰琳 on 2017/1/10.
//  Copyright © 2017年 IB. All rights reserved.
//

import XCTest

@testable import Quick
@testable import Nimble
@testable import User
@testable import ReactiveCocoa
@testable import ReactiveSwift
@testable import enum Result.NoError





extension Reactive where Base: UITextField{
    
    public var textChangedValues: Signal<String?, NoError>{
        return NotificationCenter.default.reactive.notifications(forName: .UITextFieldTextDidChange, object: self.base).map{($0.object as! UITextField).text}
    }
}


class APIPhonePwdLogin: NSObject, PhonePwdLoginService {
    
    typealias ResultType = String
    func loginWith(phone: String?, pwd: String?) -> SignalProducer<String, NSError> {
        return SignalProducer.init(value: "aaa")
    }
}




class PhonePwdLoginViewModelSpec: QuickSpec {
    
    override func spec(){
        
        var phoneField: UITextField!
        var pwdField: UITextField!
        
        var viewModel: PhonePwdLoginViewModel<APIPhonePwdLogin>!
        var service: APIPhonePwdLogin!
        
        beforeEach {
            phoneField = UITextField.init(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
            pwdField   = UITextField()
            
            let service = APIPhonePwdLogin()
            viewModel  = PhonePwdLoginViewModel(service: service)
            viewModel.phoneValidator = PhoneValidator()
        }
        
        afterEach {
            phoneField = nil
            pwdField   = nil
        }
        
        it("test nil is empty should be true", closure: {
            let str: String? = nil
            expect(str?.isEmpty).toEventually(beNil())
        })
        
        it("test string with no charactor", closure: {
            let str: String? = ""
            let r = str?.isEmpty
            expect(str?.isEmpty) == true
            
            let st: String? = "  "
            let e = st?.isEmpty
            print(e)
            
        })
        
        it("修改viewmodel的手机号 field的text也修改", closure: {
            phoneField.reactive.text <~ viewModel.phone
            viewModel.phone.value = "134"
            expect(phoneField.text) == "134"
        })
        
        
        it("修改text field, viewModel的手机号 也修改", closure: {
            viewModel.phone <~ phoneField.reactive.textChangedValues
            phoneField.text = "111"
            NotificationCenter.default.post(name: .UITextFieldTextDidChange, object: phoneField)
            expect(viewModel.phone.value) == "111"
        })
        
        
        it("手机号正常", closure: {
            viewModel.phone.value = "13761991111"
            expect(viewModel.phoneValid.value) == true
        })
        
        it("手机号不正确", closure: {
            viewModel.phone.value = "137"
            expect(viewModel.phoneValid.value) == false
        })
        
        it("密码非空", closure: {
            viewModel.password.value = "123456"
            expect(viewModel.passwordValid.value) == true
        })
        
        it("网络请求处理 异步测试", closure: {
            viewModel.phone.value = "13761992653"
            viewModel.password.value = nil
            
            var resultTxt: String? = nil
            
            viewModel.login().startWithResult({ (result) in
                switch result {
                case .success(let v):
                    resultTxt = v
                case .failure(let err):
                    print(err)
                }
            })

            expect(resultTxt).toEventually(equal("aaa"), timeout: 30)
            
        })
        
    }
}
